﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Text;
using System.Collections.Generic;

using System.Text.RegularExpressions;

public partial class Manage_Admin_AdminSiteRole : System.Web.UI.Page
{
    private KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();

    protected String act = PageRequest.GetString("act");
    protected string SiteRoleFlag = PageRequest.GetString("RoleFlag");

    protected KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();
    protected KeRui.Model.Admin modelAdmin = new KeRui.Model.Admin();
    private KeRui.BLL.Admin bllAdmin = new KeRui.BLL.Admin();
    protected int siteID = -1;
    protected int userID = -1;
    private ManageErrMsg Err = new ManageErrMsg();
    int EmptyNum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        siteID = PageRequest.GetInt("siteID", -1);
        userID = PageRequest.GetInt("userID", -1);
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        modelCmsSite = bllCmsSite.GetModel(siteID);
        if (modelCmsSite == null)
        {
            Err.AddErr("Error:siteID没有找到。"); Err.ChkErr();
        }
        modelAdmin = bllAdmin.GetModel(userID);
        if (modelAdmin == null)
        {
            Err.AddErr("Error:userID没有找到。"); Err.ChkErr();
        }
        switch (act)
        {
            case "save":
                Save();
                break;
            case "addNodeRole":
                addNodeRole();
                break;
            default:
                break;
        }
    }

    #region 保存
    private void Save()
    {
        modelAdmin.UserID = userID;
        #region 得到 modelAdmin.PlusFlag
        KeRui.Model.Admin modelAdmin_old = bllAdmin.GetModel(userID);//获取原来的modelAdminRole_old
        string SiteRoleFlag = PageRequest.GetString("RoleFlag");
        if (SiteRoleFlag == "")
        {
            SiteRoleFlag = "site_" + siteID;
        }
        else
        {
            SiteRoleFlag = "site_" + siteID + "," + SiteRoleFlag;//多了这部
        }
        string pattern = @"site\w*?_" + siteID + "";
        Regex regex = new Regex(pattern);
        MatchCollection collection = regex.Matches(modelAdmin.UserFlag);
        foreach (Match match in collection)
        {
            //把以前该网站的权限全部替换成空字符
            modelAdmin_old.PlusFlag = modelAdmin_old.PlusFlag.Replace((match.Captures[0]).ToString(), "").Replace(",,", ",");
        }
        if (modelAdmin_old.PlusFlag == "")
        {
            modelAdmin.PlusFlag = SiteRoleFlag;
        }
        else
        {
            modelAdmin.PlusFlag = modelAdmin_old.PlusFlag + "," + SiteRoleFlag;
        }
        #endregion
        modelAdmin.RoleFlags = modelAdmin_old.RoleFlags;
        #region modelAdmin.UserFlag
        modelAdmin.UserFlag = "";
        if (modelAdmin.PlusFlag == "")
        {
            if (modelAdmin.RoleFlags != "")
            {
                modelAdmin.UserFlag = modelAdmin.RoleFlags;
            }
        }
        else
        {
            if(modelAdmin.RoleFlags =="")
            {
                modelAdmin.UserFlag = modelAdmin.PlusFlag;
            }
            else
            {
                modelAdmin.UserFlag = modelAdmin.RoleFlags+","+modelAdmin.PlusFlag;
            }
        }
        #endregion


        bllAdmin.UpdatePlusFlagForSiteFlag(modelAdmin);//添加操作
        Response.Redirect("AdminSiteRole.aspx?siteid=" + siteID + "&userID=" + userID + "&sucmsg=保存成功。");
    }
    //HYC
    private void addNodeRole()
    {
        modelAdmin.UserID = userID;
        KeRui.Model.Admin modelAdmin_old = bllAdmin.GetModel(userID);//获取原来的modelAdminRole_old
        #region  获得所有的选中项，放入SiteRoleFlag
        string SiteRoleFlag = PageRequest.GetString("RoleFlag");
        if (SiteRoleFlag == "")
        {
            SiteRoleFlag = "site_" + siteID;
        }
        else
        {
            SiteRoleFlag = "site_" + siteID + "," + SiteRoleFlag;//多了这部
        }
        #endregion
        #region  删除该站点下拥有的栏目权限
        string pattern =  siteID + @"_\w*?node";
        Regex regex = new Regex(pattern);
        MatchCollection collection = regex.Matches(modelAdmin.UserFlag);
        foreach (Match match in collection)
        {
            //把以前该网站的权限全部替换成空字符
            modelAdmin_old.PlusFlag = modelAdmin_old.PlusFlag.Replace((match.Captures[0]).ToString(), "").Replace(",,", ",");
        }
        #endregion
        #region  添加选中的栏目权限  站点ID+栏目ID+"node"
        if (modelAdmin_old.PlusFlag == "")
        {
            modelAdmin.PlusFlag = SiteRoleFlag;
        }
        else
        {
            modelAdmin.PlusFlag = modelAdmin_old.PlusFlag + "," + SiteRoleFlag;
        }
        
        #endregion

        //组合完成的数据
        modelAdmin.RoleFlags = modelAdmin_old.RoleFlags;
        #region modelAdmin.UserFlag
        modelAdmin.UserFlag = "";
        if (modelAdmin.PlusFlag == "")
        {
            if (modelAdmin.RoleFlags != "")
            {
                modelAdmin.UserFlag = modelAdmin.RoleFlags;
            }
        }
        else
        {
            if (modelAdmin.RoleFlags == "")
            {
                modelAdmin.UserFlag = modelAdmin.PlusFlag;
            }
            else
            {
                modelAdmin.UserFlag = modelAdmin.RoleFlags + "," + modelAdmin.PlusFlag;
            }
        }
        #endregion


        bllAdmin.UpdatePlusFlagForSiteFlag(modelAdmin);//添加操作
        Response.Redirect("AdminSiteRole.aspx?siteid=" + siteID + "&userID=" + userID + "&sucmsg=保存成功。");
    }
    //hyc
    #endregion

    #region 权限列表

    protected string GetRoleFlagList(string RoleFlag,string PlusFlag, int siteID)
    {
        StringBuilder xs = new StringBuilder();
        DataTable dt = bllAdminFlag.GetRoleChildFlag(0, 1).Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string checkflag = "";
            if (RoleFlag.IndexOf("," + dt.Rows[i][4].ToString() + "_" + siteID + ",") != -1)
            {
                checkflag = "checked disabled";
            }
            else
            {
                if (PlusFlag.IndexOf("," + dt.Rows[i][4].ToString() + "_" + siteID + ",") != -1)
                {
                    checkflag = "checked";
                }
            }
            EmptyNum = 0;
            xs.AppendFormat("<div id=\"p{0}\"><ul class=\"ultitle\" id=\"t{0}\"><input type=\"checkbox\" name=\"RoleFlag\" id=\"{0}\"  value=\"{3}\" " + checkflag + " onclick=\"selAll(this.id)\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;<span id=\"s{0}\"onclick=\"showchildnode(this.id)\" style=\"cursor:pointer;\">{1}</span></ul>",
                dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString() + "_" + siteID);
            xs.Append(GetChildRoleFlagList(Convert.ToInt32(dt.Rows[i][0]), RoleFlag,PlusFlag, siteID));
            xs.Append("</div>");
        }
        return xs.ToString();
    }
    private string GetChildRoleFlagList(int pID, string RoleFlag, string PlusFlag, int siteID)
    {
        string xs = "";
        for (int n = 0; n < EmptyNum; n++)
        {
            xs += "　";
        }
        StringBuilder strRFlaglist = new StringBuilder();
        if (EmptyNum == 0)
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\" style=\"border:1px solid #99CCFF;\">");
        }
        else
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\">");
        }
        DataTable dtChild = bllAdminFlag.GetRoleChildFlag(pID, 1).Tables[0];
        for (int m = 0; m < dtChild.Rows.Count; m++)
        {
            string checkflag = "";
            if (RoleFlag.IndexOf("," + dtChild.Rows[m][4].ToString() + "_" + siteID + ",") != -1)
            {
                checkflag = "checked disabled";
            }
            else
            {
                if (PlusFlag.IndexOf("," + dtChild.Rows[m][4].ToString() + "_" + siteID + ",") != -1)
                {
                    checkflag = "checked";
                }
            }

            strRFlaglist.AppendFormat("<li>" + xs + "<input type=\"checkbox\"  name=\"RoleFlag\" id=\"{0}\" value=\"{3}\" " + checkflag + " onclick=\"conclick(this.id," + pID + ")\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;{1}</li>", dtChild.Rows[m][0].ToString(), dtChild.Rows[m][1].ToString(),
                dtChild.Rows[m][3].ToString().ToLower() == "" ? "item.gif" : dtChild.Rows[m][3].ToString(), dtChild.Rows[m][4].ToString() + "_" + siteID);
            if (dtChild.Rows[m]["HaveChildNav"].ToString().ToLower() == "true")
            {
                EmptyNum++;
                strRFlaglist.Append(GetChildRoleFlagList(Convert.ToInt32(dtChild.Rows[m][0]), RoleFlag,PlusFlag, siteID));
            }
        }
        strRFlaglist.Append("</ul>");
        return strRFlaglist.ToString();
    }
    #endregion




     #region 栏目权限列表
    protected string getNodeList(int pnodeid, int siteid)
    {
        StringBuilder sb = new StringBuilder();//输出HTML
        KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
        List<KeRui.Model.CmsNode> nodeList = bllCmsNode.GetListByPId(pnodeid, siteid);//获得列表
        foreach (KeRui.Model.CmsNode nodemodel in nodeList)
        { 
            string isCheck="";
            string flag = siteid + "_" + nodemodel.NodeID + "node";
        #region  显示已经拥有的权限
            KeRui.Model.Admin loadAdmin = bllAdmin.GetModel(userID);
            int num = loadAdmin.UserFlag.IndexOf(flag);
            if (num>-1)
            {
                isCheck = "checked";
            }

        #endregion
            sb.Append("<tr style=\"height:10px;\"><td style=\"height:10px;\"><input type=\"checkbox\"  name=\"RoleFlag\"  value=\"" + siteID + "_" + nodemodel.NodeID + "node" + "\"  " + isCheck + "/>" + nodemodel.NodeName + "<span style=\"color:Blue;\">(" + nodemodel.NodeID + ")</span></td></tr>");

            if(nodemodel.ChildNum>0)
            {
                sb.Append("<tr style=\"height:10px;\"><td style=\"height:10px;\"><table border=0 style=\" margin-left:30px;height:10px;\" cellpadding=\"0\" cellspacing=\"0\">");
                sb.Append(getNodeList(nodemodel.NodeID,siteid));
                sb.Append("</table></td></tr>");
            } 
        }
   
        return  sb.ToString();
    }
   
     #endregion
}
    